package com.micro.modual.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.page.PageQuery;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.core.resp.RespJsonPageData;
import com.micro.modual.entity.GoodsSku;
import com.micro.modual.entity.SeckillActivitySence;
import com.micro.modual.entity.SeckillDetail;
import com.micro.modual.extension.SeckillDetailExtension;
import com.micro.modual.extension.SeckillExtension;
import com.micro.modual.mapper.GoodsSkuMapper;
import com.micro.modual.mapper.SeckillActivitySenceMapper;
import com.micro.modual.mapper.SeckillDetailMapper;
import com.micro.modual.model.bo.seckilldetail.SeckillDetailBO;
import com.micro.modual.model.bo.seckilldetail.SeckillDetailListBO;
import com.micro.modual.model.dto.goodssku.GoodsSkuListDTO;
import com.micro.modual.model.dto.seckilldetail.SeckillDetailListDTO;
import com.micro.modual.service.SeckillDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>秒杀商品详情表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("seckillDetailService")
public class SeckillDetailServiceImpl extends AbstractBasicsServiceImpl<SeckillDetail,Long> implements SeckillDetailService {

    @Autowired
    private SeckillDetailMapper seckillDetailMapper;

    @Autowired
    private GoodsSkuMapper goodsSkuMapper;

    @Autowired
    private SeckillActivitySenceMapper seckillActivitySenceMapper;

    /**
     * <h2>查找秒杀商品详情表 列表</h2>
     * @param params 列表查询请求对象
     * @return List<SeckillDetailListDTO> {@link SeckillDetailListDTO}s  秒杀商品详情表 列表返回对象
     */
    @Override
    public RespJsonPageData<SeckillDetailListDTO> findSeckillDetailList(Map<String, Object> params) {
        PageQuery pageQuery = new PageQuery(params);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<SeckillDetail> seckillDetailList = seckillDetailMapper.findSeckillDetailList(params);
        List<SeckillDetailListDTO> seckillDetailListDTOList = JacksonUtil.getList(JacksonUtil.toJson(seckillDetailList),SeckillDetailListDTO.class);
        return new RespJsonPageData<SeckillDetailListDTO>(seckillDetailListDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());

    }

    @Override
    public RespJsonData<List<GoodsSkuListDTO>> findGoodsSkuAll(SeckillDetailListBO seckillDetailListBO) {
        List<String> skuCodeList = seckillDetailMapper.findSeckillGoodsList(seckillDetailListBO.getSeckillCode(),seckillDetailListBO.getSeckillSceneCode());
        List<GoodsSku> goodsSkuList = goodsSkuMapper.findGoodsSkuListByCategoryCode(seckillDetailListBO.getCategoryCode());
        List<GoodsSkuListDTO> goodsSkuListDTOList = SeckillDetailExtension.convertGoodsSkuListDTO(skuCodeList,goodsSkuList);
        return new RespJsonData<List<GoodsSkuListDTO>>(goodsSkuListDTOList,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>新增秒杀商品详情表详情</h2>
     * @param seckillDetailBO {@link SeckillDetailBO} 秒杀商品详情表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addSeckillDetail(SeckillDetailBO seckillDetailBO) {
        List<SeckillDetail> seckillDetailList = SeckillDetailExtension.convertAddSeckillDetail(seckillDetailBO);
        seckillDetailMapper.insertListKeySelectiveMapper(seckillDetailList);
        SeckillActivitySence seckillActivitySence = seckillActivitySenceMapper.findActivitySenceByCode(seckillDetailBO.getSeckillCode(),seckillDetailBO.getSeckillSceneCode());
        seckillActivitySence.setGoodsCount(seckillActivitySence.getGoodsCount() + seckillDetailBO.getSeckillGoodsList().size());
        seckillActivitySenceMapper.updateByPrimaryKeySelective(seckillActivitySence);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }


    /**
     * <h2>根据主键编号删除秒杀商品详情表信息</h2>
     * @param id  秒杀商品详情表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteSeckillDetail(Long id) {
        seckillDetailMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

  

}
